package com.weds.edwx .web;

import com.alibaba.fastjson.JSONObject;
import com.weds.core.utils.DateUtils;
import com.weds.core.utils.ImageUtils;
import com.weds.core.utils.StringUtils;
import com.weds.edpf.core.constant.ScmParams;
import com.weds.edwx.entity.DoorfxRealEntity;
import com.weds.edwx.entity.TempMonitorEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import javax.annotation.Resource;
import javax.validation.Valid;

import com.weds.core.base.BaseController;
import com.weds.core.annotation.Logs;
import com.weds.core.resp.JsonResult;
import com.weds.core.base.BaseCommPager;
import com.weds.core.base.BasePageSearch;


import com.weds.edwx.entity .WarnSendUserEntity;
import com.weds.edwx.service .WarnSendUserService;

/**
 * @Author sxm
 * @Description WarnSendUser管理
 * @Date 2020-02-22
 */
@RestController
@RequestMapping(value="/warnSendUser")
@Api(value = "WarnSendUser管理", description = "WarnSendUser管理")
public class WarnSendUserController extends BaseController{
	@Autowired
	private WarnSendUserService warnSendUserService;

	@Resource
	private ScmParams scmParams;
    
	private Logger log = LogManager.getLogger();

	@Logs
	@ApiOperation(value = "/获取报警记录门", notes = "获取报警记录门")
	@RequestMapping(value = "/getWarnDoorList", method = RequestMethod.POST)
	public JsonResult<List<WarnSendUserEntity>> getWarnDoorList(@RequestBody WarnSendUserEntity record) {
		List<WarnSendUserEntity> list = warnSendUserService.selectListByEntity(record);
		return succMsgData(list);
	}

	@Logs
	@ApiOperation(value = "根据门获取门的体温报警信息", notes = "根据门获取门的体温报警信息")
	@RequestMapping(value = "/getWarnHealthInfoList", method = RequestMethod.POST)
	public JsonResult<BaseCommPager<TempMonitorEntity>> getWarnHealthInfoList(@RequestBody BasePageSearch<WarnSendUserEntity> record) {
		try {
			setPageHelper(record);
			WarnSendUserEntity entity = record.getSearch();
			if (entity == null) {
				entity = new WarnSendUserEntity();
			}
			List<TempMonitorEntity> list = warnSendUserService.getWarnHealthInfoList(entity);
			return succMsgData(new BaseCommPager<TempMonitorEntity>(list));
		} catch (Exception e) {
			return failMsg("传入的日期格式有误");
		}
	}

	@Logs
	@ApiOperation(value = "查询报警记录详细信息", notes = "查询报警记录详细信息")
	@RequestMapping(value = "/selectWarnHealthInfo", method = RequestMethod.POST)
	public JsonResult<TempMonitorEntity> selectWarnHealthInfo(@RequestBody TempMonitorEntity record) {
		TempMonitorEntity entity = warnSendUserService.selectWarnHealthInfo(record);
		if (entity != null && !StringUtils.isBlank(entity.getPhotoPath())) {
			try {
				if (!StringUtils.isBlank(scmParams.getScmUrl())) {
					entity.setPhotoPath(scmParams.getScmUrl() + entity.getPhotoPath().replace("../", ""));
				} else {
					System.out.println(scmParams.getRootPath() + File.separator
							+ entity.getPhotoPath());
					entity.setPhotoPath(ImageUtils.imgToBase64(scmParams.getRootPath() + File.separator
							+ entity.getPhotoPath().replace("../","")));
				}
				// edDineFoodEntity.setFoodPhoto("./" + edDineFoodEntity.getFoodPhoto());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return succMsgData(entity);
	}

	@Logs
	@ApiOperation(value = "提交报警处理记录", notes = "提交报警处理记录")
	@RequestMapping(value = "/updateWarnHealthInfo", method = RequestMethod.POST)
	public JsonResult<Object> updateWarnHealthInfo(@RequestBody TempMonitorEntity record) {
		warnSendUserService.updateWarnHealthInfo(record);
		return succMsg();
	}




    @Logs
	@ApiOperation(value = "新增WarnSendUser信息", notes = "新增WarnSendUser信息")
	@RequestMapping(value = "/insert", method = RequestMethod.PUT)
	public JsonResult<Object> insert(@RequestBody @Valid WarnSendUserEntity record) {
        				warnSendUserService.insertSelective(record);
		return succMsg();
	}

	@Logs
	@ApiOperation(value = "更新WarnSendUser信息", notes = "更新WarnSendUser信息")
	@RequestMapping(value = "/updateByPrimaryKey", method = RequestMethod.POST)
	public JsonResult<Object> updateByPrimaryKey(@RequestBody @Valid WarnSendUserEntity record) {
        		warnSendUserService.updateByPrimaryKeySelective(record);
		return succMsg();
	}

	@Logs
	@ApiOperation(value = "查询WarnSendUser清单", notes = "查询WarnSendUser清单")
	@RequestMapping(value = "/selectListPageByEntity", method = RequestMethod.POST)
	public JsonResult<BaseCommPager<WarnSendUserEntity>> selectListPageByEntity(@RequestBody BasePageSearch<WarnSendUserEntity> record) {
		setPageHelper(record);
		WarnSendUserEntity entity = record.getSearch();
        if (entity == null) {
        	entity = new WarnSendUserEntity();
        }
                List<WarnSendUserEntity> list = warnSendUserService.selectListByEntity(entity);
        		return succMsgData(new BaseCommPager<WarnSendUserEntity>(list));
	}

	@Logs
	@ApiOperation(value = "删除WarnSendUser信息", notes = "删除WarnSendUser信息")
	@RequestMapping(value = "/deleteByPrimaryKey", method = RequestMethod.DELETE)
	public JsonResult<Object> deleteByPrimaryKey(@RequestParam Long xh) {
        warnSendUserService.deleteByPrimaryKey(xh);
        return succMsg();
	}

	@Logs
	@ApiOperation(value = "查询WarnSendUser信息", notes = "查询WarnSendUser信息")
	@RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.GET)
	public JsonResult<WarnSendUserEntity> selectByPrimaryKey(@RequestParam Long xh) {
		WarnSendUserEntity entity = warnSendUserService.selectByPrimaryKey(xh);
                return succMsgData(entity);
	}
	

		@Logs
	@ApiOperation(value = "批量删除WarnSendUser信息", notes = "批量删除WarnSendUser信息")
	@RequestMapping(value = "/deleteBatchByKeys", method = RequestMethod.POST)
	public JsonResult<Object> deleteBatchByKeys(@RequestBody List<WarnSendUserEntity> list) {
        List<WarnSendUserEntity> delList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            delList.add(list.get(i));
            if (delList.size() == 20 || i == list.size() - 1) {
                warnSendUserService.deleteBatchByKeys(delList);
                delList.clear();
            }
        }
		return succMsg();
	}
	
		@Logs
	@ApiOperation(value = "批量新增WarnSendUser信息", notes = "批量新增WarnSendUser信息")
	@RequestMapping(value = "/insertBatch", method = RequestMethod.PUT)
	public JsonResult<Object> insertBatch(@RequestParam String data) {
		List<WarnSendUserEntity> list	= JSONObject.parseArray(data, WarnSendUserEntity.class);
		warnSendUserService.insertBatch(list);
		return succMsg();
	}
	}